import moment from "moment"
/**
 * 通用js方法封装处理
 * Copyright (c) 2019 ruoyi
 */
const baseURL = process.env.VUE_APP_BASE_API
const dateFormat = 'yyyy-MM-DD HH:mm:ss'
// 日期格式化 - 转化成目标时区 -当前时区
export function parseTimeZoneToApi(time, timeZoneOffset) {
  var res = moment(time).utc();
  return res;
}
// 排序方法
export function compare(property) {//property:根据什么属性排序
  return function (a, b) {
    var value1 = a[property];
    var value2 = b[property];
    /*
    * value2 - value1; ——> 降序
    * value1 - value2; ——> 升序
    */
    return value1 - value2;//升序排序
  }
}

export function getMinsIntervalByDateTime(queryStart, queryEnd) {
  var res = 1;
  var diffDateTime = moment(queryEnd).diff(moment(queryStart), 'days');
  if (diffDateTime <= 1) {
    res = 15
  } else
    if (res > 1 && res <= 3) {

      res = 60
    } else
      if (res > 3 && res <= 7) {

        res = 240
      } else
        if (res > 10 && res == 12) {

          res = 720
        }
        else {
          res = 1400
        }
  return res;


}
export function formatMin(mins, lang) {
  let x = mins * 60;//单位是秒
  var d = moment.duration(x, 'seconds');

  var res = "";
  if (lang == "zh") {

    if (mins == 0) {

      res = '0天0时0分';
    }
    else {

      res = Math.floor(d.asDays()) + '天' + d.hours() + '时' + d.minutes() + '分';
    }


  }
  else {
    if (mins == 0) {

      res = '0 Days 0 Hrs 0 Mins';
    } else {

      res = Math.floor(d.asDays()) + ' Days ' + d.hours() + ' Hrs ' + d.minutes() + ' Mins';
    }


  }

  return res;
}

export function formatMinHour(mins, lang) {


  let x = mins * 60 * 60;//单位是秒


  var d = moment.duration(x, 'seconds');

  var res = "";
  if (lang == "zh") {
    res = Math.floor(d.asDays()) + '天' + d.hours() + '时' + d.minutes() + '分';

  }
  if (lang == "1") {
    res = Math.floor(d.asDays()) + ' Days ' + String(d.hours()) + ' Hrs ' + String(d.minutes()) + ' Mins';
  }
  return res;
}

// 日期格式化 - 转化成目标时区可显示  8小时
export function parseTimeZoneToDisplay(time) {
  var strutcOffset = new Date().getTimezoneOffset()
  if (strutcOffset == 0) {
    return moment(time).format("yyyy-MM-DD HH:mm");
  }
  else {
    if (arguments.length === 0 || !time) {
      return null
    }
    let timeZone = 8;//getTimeZone();
    var res = moment.utc(time).utcOffset(Number(timeZone) * 60).format("yyyy-MM-DD HH:mm");

    return res;

  }
}
export function parseTimeZoneToDisplay1(time) {
  var strutcOffset = new Date().getTimezoneOffset()
  if (strutcOffset == 0) {
    return moment(time).format("yyyy-MM-DD");
  }
  else {
    if (arguments.length === 0 || !time) {
      return null
    }
    let timeZone = 8;//getTimeZone();
    var res = moment.utc(time).utcOffset(Number(timeZone) * 60).format("yyyy-MM-DD");

    return res;

  }
}
export function parseTimeZoneToDisplay2(time) {
  var strutcOffset = new Date().getTimezoneOffset()
  if (strutcOffset == 0) {

    return moment(time).format("yyyy-MM-DD HH:mm:ss");

  }
  else {

    if (arguments.length === 0 || !time) {
      return null
    }

    let timeZone = 8;//getTimeZone();


    var res = moment.utc(time).utcOffset(Number(timeZone) * 60).format("yyyy-MM-DD HH:mm:ss");

    return res;

  }

}

export function parseTimeZoneToDisplay4(time) {


  var strutcOffset = new Date().getTimezoneOffset()
  if (strutcOffset == 0) {

    return moment(time).format("YYYYMMDDHHMM");

  }
  else {

    if (arguments.length === 0 || !time) {
      return null
    }

    let timeZone = 8;//getTimeZone();


    var res = moment.utc(time).utcOffset(Number(timeZone) * 60).format("YYYYMMDDHHMM");

    return res;

  }

}

export function parseTimeZoneToDisplay3(time) {
  var strutcOffset = new Date().getTimezoneOffset()
  if (strutcOffset == 0) {
    return moment(time).format("HH:mm:ss");
  }
  else {
    if (arguments.length === 0 || !time) {
      return null
    }
    let timeZone = 8;//getTimeZone();
    var res = moment.utc(time).utcOffset(Number(timeZone) * 60).format("HH:mm:ss");
    return res;
  }

}

export function parseTimeZoneToDisplay5(time) {
  var strutcOffset = new Date().getTimezoneOffset()
  if (strutcOffset == 0) {
    return moment(time).format("HH:mm");
  }
  else {
    if (arguments.length === 0 || !time) {
      return null
    }
    let timeZone = 8;//getTimeZone();
    var res = moment.utc(time).utcOffset(Number(timeZone) * 60).format("HH:mm");
    return res;
  }

}

export function isBefore(time, toBeComparedTime) {
  return moment(time).isBefore(toBeComparedTime)
}

export function changeDecimalBuZero(number, bitNum) {
  /// <summary>
  /// 小数位不够，用0补足位数
  /// </summary>
  /// <param name="number">要处理的数字</param>
  /// <param name="bitNum">生成的小数位数</param>
  var f_x = parseFloat(number);
  if (isNaN(f_x)) {
    return 0;
  }
  var s_x = number.toString();
  var pos_decimal = s_x.indexOf('.');
  if (pos_decimal < 0) {
    pos_decimal = s_x.length;
    s_x += '.';
  }
  while (s_x.length <= pos_decimal + bitNum) {
    s_x += '0';
  }
  return s_x;
}

//格式化数字 s：要格式化的数字  n：保留几位小数
export function formatNumber(s, n) {

  if (!n) {
    n = 1
  } else {
    n = n > 0 && n <= 20 ? n : 2;
  }
  s = parseFloat((s + "").replace(/[^\d\.-]/g, "")).toFixed(n) + "";
  let l = s.split(".")[0].split("").reverse(),
    r = s.split(".")[1];
  let t = "";
  for (let i = 0; i < l.length; i++) {
    t += l[i] + ((i + 1) % 3 === 0 && (i + 1) != l.length ? "," : "");
  }
  var str = t.split("").reverse().join("") + "." + r;

  if (str.substring(0, 1) == '-') {

    if (str.substring(1, 2) == ',') {

      var res = str.substring(0, 1) + str.substring(2);
      return res;
    }
    else {

      return str;
    }
  }
  else {
    return str;

  }


}


export function toDecimal2(x) {
  var f = parseFloat(x);
  if (isNaN(f)) {
    return false;
  }
  var f = Math.round(x * 100) / 100;
  var s = f.toString();
  var rs = s.indexOf('.');
  if (rs < 0) {
    rs = s.length;
    s += '.';
  }
  while (s.length <= rs + 2) {
    s += '0';
  }
  return s;
}

export function toDecimal(x, c) {

  var f = parseFloat(x);
  if (isNaN(f)) {
    return false;
  }
  var f = Math.round(x * 100) / 100;
  var s = f.toString();
  var rs = s.indexOf('.');
  if (rs < 0) {
    rs = s.length;
    s += '.';
  }
  while (s.length <= rs + c) {
    s += '0';
  }
  return s;
}
// 字符串格式化(%s )
export function sprintf(str) {
  var args = arguments, flag = true, i = 1;
  str = str.replace(/%s/g, function () {
    var arg = args[i++];
    if (typeof arg === 'undefined') {
      flag = false;
      return '';
    }
    return arg;
  });
  return flag ? str : '';
}

// 转换字符串，undefined,null等转化为""
export function praseStrEmpty(str) {
  if (!str || str == "undefined" || str == "null") {
    return "";
  }
  return str;
}


export function insertStr(soure, start, newStr) {
  return soure.slice(0, start) + newStr + soure.slice(start);
}
/**
 * 参数处理
 * @param {*} params  参数
 */
export function tansParams(params) {
  let result = ''
  Object.keys(params).forEach((key) => {
    if (!Object.is(params[key], undefined) && !Object.is(params[key], null) && !Object.is(JSON.stringify(params[key]), '{}')) {
      result += encodeURIComponent(key) + '=' + encodeURIComponent(params[key]) + '&'
    }
  })
  return result
}
export function formatTime(value) {
  //解决invalid date
  value = value.replace(new RegExp(/-/gm), "/");
  var date = new Date(value);

  Y = date.getFullYear(),
    m = date.getMonth() + 1,
    d = date.getDate(),
    H = date.getHours(),
    i = date.getMinutes(),
    s = date.getSeconds();
  if (m < 10) {
    m = '0' + m;
  }
  if (d < 10) {
    d = '0' + d;
  }
  if (H < 10) {
    H = '0' + H;
  }
  if (i < 10) {
    i = '0' + i;
  }
  if (s < 10) {
    s = '0' + s;
  }
  var t = Y + '-' + m + '-' + d + ' ' + H + ':' + i + ':' + s;
  return t;
}

export function handleInput(e) {

  e.target.value = (e.target.value.match(/^\d*(\.?\d{0,1})/g)[0]) || null

}
export function handleInput7(e) {

  e.target.value = (e.target.value.match(/^\d*(\.?\d{0,7})/g)[0]) || null

}
export function handleInput2(e) {

  e.target.value = (e.target.value.match(/^\d*(\.?\d{0,2})/g)[0]) || null

}
// 生成随机数
export function randomFun(start, end) {
  return Math.round(Math.random() * (end - start) + start)
}



export function formatEveryDay(start, end) {
  let dateList = [];
  var startTime = getDate(start);
  var endTime = getDate(end);

  while ((endTime.getTime() - startTime.getTime()) >= 0) {
    var year = startTime.getFullYear();
    var month = startTime.getMonth() + 1 < 10 ? '0' + (startTime.getMonth() + 1) : startTime.getMonth() + 1;
    var day = startTime.getDate().toString().length == 1 ? "0" + startTime.getDate() : startTime.getDate();
    dateList.push(year + "-" + month + "-" + day);
    startTime.setDate(startTime.getDate() + 1);
  }
  return dateList;
}

export function formatEveryHourOrMins(start, end, mins) {
  var minutes = moment(end).diff(moment(start), 'minutes')
  var arry = [];
  for (var i = 0; i < minutes / mins; i++) {
    var res = moment(start).subtract(i * mins, 'minutes').format("HH:mm");
    arry.unshift(res);
  }
  return arry;
}

export function numberWithCommas(e) {
  //因为这个有千分位分隔符要去掉

  let filterValue = e.target.value.replace(/,/g, '')
  let value = Number(filterValue)
  if (e.target.value) {
    if (isNaN(value) || typeof value !== 'number') {
      e.target.value = null
    }
  }
}
export function setColor(val) {
  if (val >= 0.0 && val <= 0.2) {
    return 'green';
  }
  if (val >= 0.3 && val <= 1.5) {
    return 'green';
  }
  if (val >= 1.6 && val <= 3.3) {
    return 'green';
  }
  if (val >= 3.4 && val <= 5.4) {
    return 'green';
  }

  if (val >= 5.5 && val <= 7.9) {
    return 'yellow';
  }



  if (val >= 8.0 && val <= 10.7) {
    return 'red1';
  }


  if (val >= 10.8 && val <= 13.8) {
    return 'red1'; 6;
  }
  if (val >= 13.9 && val <= 17.1) {
    return 'red2';
  }
  if (val >= 17.2 && val <= 20.7) {
    return 'red2';
  }
  if (val >= 20.8 && val <= 24.4) {
    return 'red2';
  }
  if (val >= 24.5 && val <= 28.4) {
    return 'red2';
  }

  if (val >= 28.5 && val <= 32.6) {
    return 'red2';
  }

  if (val > 32.6) {
    return 'red2';
  }

}
export function getWindLevel(val) {
  if (val >= 0.0 && val <= 0.2) {
    return 0;
  }
  if (val >= 0.3 && val <= 1.5) {
    return 1;
  }
  if (val >= 1.6 && val <= 3.3) {
    return 2;
  }
  if (val >= 3.4 && val <= 5.4) {
    return 3;
  }

  if (val >= 5.5 && val <= 7.9) {
    return 4;
  }



  if (val >= 8.0 && val <= 10.7) {
    return 5;
  }


  if (val >= 10.8 && val <= 13.8) {
    return 6;
  }
  if (val >= 13.9 && val <= 17.1) {
    return 7;
  }
  if (val >= 17.2 && val <= 20.7) {
    return 8;
  }
  if (val >= 20.8 && val <= 24.4) {
    return 9;
  }
  if (val >= 24.5 && val <= 28.4) {
    return 10;
  }

  if (val >= 28.5 && val <= 32.6) {
    return 11;
  }

  if (val > 32.6) {
    return 12;
  }

};
export function getWindName(val) {
  if (val >= 0.0 && val <= 0.2) {
    return this.$t("wind.Quietwind");
  }
  if (val >= 0.3 && val <= 1.5) {
    return this.$t("wind.Lightwind");
  }
  if (val >= 1.6 && val <= 3.3) {
    return this.$t("wind.Lightbreeze");
  }
  if (val >= 3.4 && val <= 5.4) {
    return this.$t("wind.Breeze");
  }


  if (val >= 5.5 && val <= 7.9) {
    return this.$t("wind.Gentlebreeze");
  }



  if (val >= 8.0 && val <= 10.7) {
    return this.$t("wind.Strongbreeze");
  }


  if (val >= 10.8 && val <= 13.8) {
    return this.$t("wind.Strongwind");
  }
  if (val >= 13.9 && val <= 17.1) {
    return this.$t("wind.Blast");
  }
  if (val >= 17.2 && val <= 20.7) {
    return this.$t("wind.Gale");
  }
  if (val >= 20.8 && val <= 24.4) {
    return this.$t("wind.StrongGale");
  }
  if (val >= 24.5 && val <= 28.4) {
    return this.$t("wind.Wholegale");
  }

  if (val >= 28.5 && val <= 32.6) {
    return this.$t("wind.Storm");
  }

  if (val > 32.6) {
    return this.$t("wind.Hurricane");
  }


};
export function launchFullScreen(id) {
  let element = document.getElementById(id);
  if (this.FullScreen) {
    if (document.exitFullscreen) {
      document.exitFullscreen();
    } else if (document.webkitCancelFullScreen) {
      document.webkitCancelFullScreen();
    } else if (document.mozCancelFullScreen) {
      document.mozCancelFullScreen();
    } else if (document.msExitFullscreen) {
      document.msExitFullscreen();
    }
  } else {
    if (element.requestFullscreen) {
      element.requestFullscreen();
    } else if (element.webkitRequestFullScreen) {
      element.webkitRequestFullScreen();
    } else if (element.mozRequestFullScreen) {
      element.mozRequestFullScreen();
    } else if (element.msRequestFullscreen) {
      element.msRequestFullscreen();
    }
  }
  this.FullScreen = !this.FullScreen;
}

export const operationModelOptions = [
  {
    value: '全部',
    label: '全部'
  }, {
    value: '靠泊',
    label: '靠泊'
  }, {
    value: '离泊',
    label: '离泊'
  }
  , {
    value: '调遣',
    label: '调遣'
  }
  , {
    value: '锚泊',
    label: '锚泊'
  }

  , {
    value: '接引水',
    label: '接引水'
  }
  , {
    value: '送引水',
    label: '送引水'
  }
  , {
    value: '航行(长航)',
    label: '航行(长航)'
  }
  , {
    value: '日常作业',
    label: '日常作业',
    children: [{
      value: '接送引水',
      label: '接送引水',
    },
    {
      value: '护航(日常生产)',
      label: '护航(日常生产)',
    },
    {
      value: '移泊',
      label: '移泊',
    },
    {
      value: '备车待命',
      label: '备车待命',
    },
    {
      value: '漂航待命',
      label: '漂航待命',
    },
    {
      value: '停车待命',
      label: '停车待命',
    },
    {
      value: '停靠码头(靠他船)',
      label: '停靠码头(靠他船)',
    }

    ]
  },
  {
    value: '特殊作业',
    label: '特殊作业',
    children: [
      {
        value: '抢险救助',
        label: '抢险救助'
      },
      {
        value: '协助脱浅',
        label: '协助脱浅'
      }
      ,
      {
        value: '拖带',
        label: '拖带'
      }
      ,
      {
        value: '应急(含危险品)拖带',
        label: '应急(含危险品)拖带'
      }
      ,
      {
        value: '水上交通',
        label: '水上交通'
      }
      ,
      {
        value: '巡查水域(航道)',
        label: '巡查水域(航道)'
      }
      ,
      {
        value: '接人员/物品',
        label: '接人员/物品'
      }
      ,
      {
        value: '送人员/物品',
        label: '送人员/物品'
      }
      ,
      {
        value: '协助抛锚',
        label: '协助抛锚'
      }

      ,
      {
        value: '水上定位',
        label: '水上定位'
      }

      ,
      {
        value: '船舶消防',
        label: '船舶消防'
      }

      ,
      {
        value: '协助调头',
        label: '协助调头'
      }

      ,
      {
        value: '系浮筒',
        label: '系浮筒'
      }
      ,
      {
        value: '解浮筒',
        label: '解浮筒'
      }
      ,
      {
        value: '危险品监护',
        label: '危险品监护'
      }
      ,
      {
        value: '消防监护',
        label: '消防监护'
      }
      ,
      {
        value: '安全监护',
        label: '安全监护'
      }

      ,
      {
        value: '安全性护航',
        label: '安全性护航'
      }
      ,
      {
        value: '船厂租用',
        label: '船厂租用'
      }
      ,
      {
        value: '助船进坞',
        label: '助船进坞'
      }
      ,
      {
        value: '助船出坞',
        label: '助船出坞'
      }
    ]
  },
  {
    value: '其他',
    label: '其他',
    children: [
      {
        value: '演练',
        label: '演练'
      }
      ,
      {
        value: '外协',
        label: '外协'
      }
      ,
      {
        value: '让档',
        label: '让档'
      }
      ,
      {
        value: '厂修',
        label: '厂修'
      }
      ,
      {
        value: '航修',
        label: '航修'
      }
      ,
      {
        value: '租赁',
        label: '租赁'
      }
      ,
      {
        value: '内部租赁',
        label: '内部租赁'
      }

    ]
  }
]

export function tooltipFormatter(val){
  var arr = val.split('<br>');
  var strRes = "";
  strRes += '<div style="color:#00B0F0;font-weight:bold;">' + arr[0] + '</div>';      //0
  strRes += '<div >' + arr[1] + '</div>';   //1
  strRes += '<div ><span style="color:#00B0F0;">' + arr[2].split(':')[0].split('(')[0] + '</span><span>('+ arr[2].split(':')[0].split('(')[1] +'</span> : <span style="color:yellow">' + arr[2].split(':')[1] + '</span></div>';     //2
  strRes += '<div ><span style="color:#00B0F0;">' + arr[3].split(':')[0].split('(')[0] + '</span><span>('+ arr[3].split(':')[0].split('(')[1] +'</span> : <span style="color:yellow">' + arr[3].split(':')[1] + '</span></div>';           //3
  strRes += '<div ><span style="color:#00B0F0;">' + arr[4].split(':')[0].split('(')[0] + '</span><span>('+ arr[4].split(':')[0].split('(')[1] +'</span> : <span style="color:yellow">' + arr[4].split(':')[1] + '</span></div>';    
  strRes += '<div ><span style="color:#00B0F0;">' + arr[5].split(':')[0].split('(')[0] + '</span><span>('+ arr[5].split(':')[0].split('(')[1] +'</span> : <span style="color:yellow">' + arr[5].split(':')[1].split('(')[0] + '</span><span>('+arr[5].split(':')[1].split('(')[1]+'</span></div>';       //5
  strRes += '<div ><span style="color:#00B0F0;">' + arr[6].split(':')[0].split('(')[0] + '</span><span>('+ arr[6].split(':')[0].split('(')[1] +'</span> : <span style="color:yellow">' + arr[6].split(':')[1].split('(')[0] + '</span><span>('+ arr[6].split(':')[1].split('(')[1]+'</span></div>';    
  strRes += '<div ><span style="color:#00B0F0;">' + arr[7].split(':')[0] + '</span> : <span style="color:yellow">' + arr[7].split(':')[1] + '</span></div>';     //7

  return strRes;
}